//
//  FNAVListViewController.m
//  Video练习
//
//  Created by haoran on 16/4/11.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "FNAVListViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "FNAVListItem.h"
#import "FNAVListCell.h"
#import <MJRefresh/MJRefresh.h>

static NSString * ID = @"cell";

@interface FNAVListViewController ()

/** 模型数组 **/
@property (nonatomic, strong) NSMutableArray * itemArray;
/** 起始编号 **/
@property (nonatomic, assign) NSInteger starNum;
/** 结束编号 **/
@property (nonatomic, assign) NSInteger endNum;
@end

@implementation FNAVListViewController



#pragma mark - 懒加载
-(NSMutableArray *)itemArray{
    if (_itemArray == nil) {
        _itemArray = [NSMutableArray array];
    }

    return _itemArray;
}


- (void)viewDidLoad {
    
    self.starNum = 1;
    self.endNum = 10;
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
    
    [self getAVListWithTid:self.tid :^(NSArray *array) {
        
        [self.itemArray addObjectsFromArray: array];
        [self.tableView reloadData];
        
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FNAVListCell" bundle:nil] forCellReuseIdentifier:ID];
    
    // 设置header
     self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAllColumns:)];
     [self.tableView.mj_header beginRefreshing];
    // 设置footer
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

-(void)loadMoreData{
    
    //确定URL
    NSString *urlstr = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/Tlist/%@/%zd-%zd.html",self.tid,self.starNum,self.endNum];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *dictArray = responseObject[self.tid];
        
       [self.itemArray addObjectsFromArray:[FNAVListItem mj_objectArrayWithKeyValuesArray:dictArray]];
  
        
        [self.tableView reloadData];
        self.starNum += 10;
        self.endNum += 10;
        
        
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

    
    
 
}
// 获取每个子页面的标题和节点
+(void)getAllColumns:(void (^)(NSArray *))complete{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    [manager GET:@"http://c.m.163.com/nc/video/topiclist.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSMutableArray * responseObject) {
        
//        NSLog(@"%@---%@",[responseObject class],responseObject);
        
        complete(responseObject);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
}

// 根据节点,获取每个页面的数据
-(void)getAVListWithTid :(NSString *)tid :(void (^)(NSArray *))complete{
    
    //确定URL
    NSString *urlstr = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/Tlist/%@/%zd-%zd.html",tid,self.starNum,self.endNum];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

    [manager GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *dictArray = responseObject[tid];
        
        NSArray * itemsArray = [FNAVListItem mj_objectArrayWithKeyValuesArray:dictArray];
        complete(itemsArray);
        
        self.starNum += 10;
        self.endNum += 10;
        
        [self.tableView.mj_header endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}



#pragma mark - 代理
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"选中了第@%zd",indexPath.row);
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.itemArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FNAVListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FNAVListItem *item = self.itemArray[indexPath.row];
    cell.item = item;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FNAVListItem *item = self.itemArray[indexPath.row];
    return item.cellHeight;
//    return 320;
}


// 设置footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
// 设置footer样式
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footV = [[UIView alloc] init];
    footV.backgroundColor = FNColor(215, 215, 215);
    footV.bounds = CGRectMake(0, 0, FNScreenW, 10);
    return footV;
}


@end
